package com.lzy.cloud.goods.facade;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzy.cloud.goods.constant.GoodsParam;
import com.lzy.cloud.goods.dtos.*;
import com.lzy.cloud.goods.remote.ShopApi;
import com.lzy.cloud.goods.remote.dtos.ShopDto;
import com.lzy.cloud.goods.response.*;
import com.lzy.cloud.goods.service.*;
import com.lzy.cloud.goods.service.carRent.CarRentService;
import com.lzy.cloud.goods.service.carRent.InsuranceService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DateUtil;
import com.lzy.framework.core.utils.DozerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 商品总Facade
 *
 * @author lc
 * @version 2018/7/17
 */
@Component
@Slf4j
public class GoodsFacade {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ParamService paramService;

    @Autowired
    private CarParamService carParamService;

    @Autowired
    private ConfigService configService;

    @Autowired
    private CarConfigService carConfigService;

    @Autowired
    private CarImageService carImageService;

    @Autowired
    private InsuranceService insuranceService;

    @Autowired
    private CarDraftService carDraftService;

    @Autowired
    private CarRentService carRentService;

    @Autowired
    private CarSaleService carSaleService;

    @Autowired
    private ParamFacade paramFacade;

    @Autowired
    private CarVideoService carVideoService;

    @Autowired
    private ShopApi shopApi;

    public Boolean addGoods(GoodsDto dto,Long shopId) {
        Ensure.that(dto).isNotNull("00010001");
        dto.setShopId(shopId);
        switch (dto.getType()) {
            case GoodsParam.CAR_SALE: {
                //房车销售
//                dto = DozerUtils.map(dto.getCarSaleDto(),GoodsDto.class);
                dto.setCode(getCode(GoodsParam.CAR_SALE, shopId));
                dto.setName(dto.getCarSaleDto().getName());
                dto.setOrigPrice(dto.getCarSaleDto().getPrice());
                dto.setSellPrice(dto.getCarSaleDto().getPrice());
                dto.setAgentPrice(dto.getCarSaleDto().getSupplyPrice());
                dto.setImage(dto.getCarSaleDto().getImage());
                dto.setDescribes(dto.getCarSaleDto().getDescribes());
                dto.setRemark(dto.getCarSaleDto().getRemark());
                dto.setDraft(dto.getCarSaleDto().getDraft());
                Long id = goodsService.add(dto);
                Ensure.that(id).isNotNull("00010001");
                dto.getCarSaleDto().setCreateUser(dto.getCreateUser());
                dto.getCarSaleDto().setShopId(shopId);
                addCarSaleMessage(id, dto.getCarSaleDto());
                break;
            }
            case GoodsParam.CAR_RENT: {
                //房车租赁
                dto.setCode(getCode(GoodsParam.CAR_RENT, shopId));
                dto.setName(dto.getCarRentDto().getName());
                dto.setOrigPrice(dto.getCarRentDto().getPrice());
                dto.setSellPrice(dto.getCarRentDto().getPrice());
                dto.setImage(dto.getCarRentDto().getImage());
                dto.setDescribes(dto.getCarRentDto().getDescribes());
                dto.setRemark(dto.getCarRentDto().getRemark());
                dto.setDraft(dto.getCarRentDto().getDraft());
                Long id = goodsService.add(dto);
                Ensure.that(id).isNotNull("00010001");
                dto.getCarRentDto().setCreateUser(dto.getCreateUser());
                dto.getCarRentDto().setShopId(shopId);
                addCarRentMessage(id, dto.getCarRentDto(), dto.getCreateUser(), null);
                break;
            }
        }
        return true;
    }

    private Boolean addCarSaleMessage(Long id, CarSaleDto dto) {
        //新增房车基本参数
        if (null != dto.getCarParamDtoList() && dto.getCarParamDtoList().size() > 0) {
            //判断自定义参数不能超过10个
            ParamDto p = new ParamDto();
            p.setMode(GoodsParam.PARAM_COMMON);
            List<ParamDto> ps = paramService.selectList(p);
            Ensure.that(dto.getCarParamDtoList().size()).isNotGt(10 + ps.size(), "00020002");

            for (CarParamDto carParamDto : dto.getCarParamDtoList()) {
                Long paramId = 0L;
                //如果这条为自定义参数
                Ensure.that(carParamDto.getParamDto().getMode()).isNotNull("00090005");
                if (carParamDto.getParamDto().getMode() == GoodsParam.PARAM_CUSTOM) {
                    //自定义参数,新增
                    ParamDto paramDto = carParamDto.getParamDto();
                    paramDto.setMode(2);
                    paramDto.setSourceId(id);
                    paramDto.setSourceType(GoodsParam.CAR_SALE);
                    paramId = paramService.add(paramDto);
                } else if (carParamDto.getParamDto().getMode() == GoodsParam.PARAM_COMMON) {
                    //公共参数,查询
                    ParamDto paramDto = paramService.selectBySourceId(carParamDto.getParamDto());
                    paramId = paramDto.getId();
                } else {
                    return false;
                }
                carParamDto.setParamId(paramId);
                carParamDto.setSourceType(GoodsParam.CAR_SALE);
                carParamDto.setSourceId(id);
                if (null != dto.getCreateUser()) {
                    carParamDto.setCreateUser(dto.getCreateUser());
                }
                if (null != dto.getUpdateUser()) {
                    carParamDto.setUpdateUser(dto.getUpdateUser());
                }
                carParamService.add(carParamDto);
            }
        }
        //新增车内配置(固定/自定义)
        List<CarConfigDto> carConfigDtoList = dto.getCarConfigDtoList();

        if (null != carConfigDtoList && carConfigDtoList.size() > 0) {
            //自定义配置最多10个
            ConfigDto config = new ConfigDto();
            config.setMode(GoodsParam.PARAM_CUSTOM);
            PageInfo<ConfigDto> pager = configService.selectList(new Pager(), config);
            Ensure.that(dto.getCarConfigDtoList().size()).isNotGt(10 + pager.getList().size(), "00020003");
            //新增到配置表
            for (CarConfigDto carConfigDto : carConfigDtoList) {
                Long configId = 0L;
                String configName = "未定义";
                //如果这条为自定义配置
                if (carConfigDto.getConfigDto().getMode() == GoodsParam.PARAM_COMMON) {
                    //如果这条为自定义配置
                    ConfigDto configDto = carConfigDto.getConfigDto();
                    configDto.setMode(2);
                    configDto.setSourceType(GoodsParam.CAR_SALE);
                    configDto.setCarId(id);
                    configId = configService.add(configDto);
                    configName = configDto.getName();
                } else if (carConfigDto.getConfigDto().getMode() == GoodsParam.PARAM_COMMON) {
                    //公共参数
                    ConfigDto configDto = configService.selectByPrimaryKey(carConfigDto.getConfigDto().getId());
                    if (configDto != null) {
                        configId = configDto.getId();
                        configName = configDto.getName();
                    }
                } else {
                    return false;
                }
                carConfigDto.setName(carConfigDto.getConfigDto().getName());
                carConfigDto.setConfigId(configId);
                carConfigDto.setSourceType(GoodsParam.CAR_SALE);
                carConfigDto.setSourceId(id);
                if (null != dto.getCreateUser()) {
                    carConfigDto.setCreateUser(dto.getCreateUser());
                }
                if (null != dto.getUpdateUser()) {
                    carConfigDto.setUpdateUser(dto.getUpdateUser());
                }
                carConfigService.add(carConfigDto);
            }
        }

        //新增车型主图
        if (null != dto.getCarImageDtoList() && dto.getCarImageDtoList().size() > 0) {
            for (CarImageDto carImageDto : dto.getCarImageDtoList()) {
                carImageDto.setSourceType(GoodsParam.PARAM_CUSTOM);
                carImageDto.setSourceId(id);
                if (null != dto.getCreateUser()) {
                    carImageDto.setCreateUser(dto.getCreateUser());
                }
                if (null != dto.getUpdateUser()) {
                    carImageDto.setUpdateUser(dto.getUpdateUser());
                }
                carImageService.add(carImageDto);
            }
        }

        //新增车型视频
        if (null != dto.getCarVideoDtoList() && dto.getCarVideoDtoList().size() > 0) {
            for (CarVideoDto carVideoDto : dto.getCarVideoDtoList()) {
                carVideoDto.setSourceType(GoodsParam.CAR_RENT);
                carVideoDto.setSourceId(id);
                if (null != dto.getCreateUser()) {
                    carVideoDto.setCreateUser(dto.getCreateUser());
                }
                if (null != dto.getCreateUser()) {
                    carVideoDto.setUpdateUser(dto.getCreateUser());
                }
                carVideoService.add(carVideoDto);
            }
        }

        //保存为草稿
        if (null != dto.getDraft() && dto.getDraft() == 1) {
            CarDraftDto carDraftDto = DozerUtils.map(dto, CarDraftDto.class);
            carDraftDto.setName(dto.getDraftName());
            carDraftDto.setShopId(dto.getShopId());
            carDraftDto.setSourceType(GoodsParam.PARAM_CUSTOM);
            carDraftDto.setSourceId(id);
            if (null != dto.getCreateUser()) {
                carDraftDto.setCreateUser(dto.getCreateUser());
            }
            if (null != dto.getUpdateUser()) {
                carDraftDto.setUpdateUser(dto.getUpdateUser());
            }
            carDraftService.add(carDraftDto);
        }
        //设置外键属性新增到租赁表
        dto.setId(id);
        //如果在数据库中查询到该ID存在的为更新
        CarSaleDto crDto = carSaleService.selectById(id);
        if (crDto != null) {
            return carSaleService.update(dto);
        } else {
            return carSaleService.add(dto) > 0;
        }
    }

    private Boolean addCarRentMessage(Long id, CarRentDto dto, Long createUser, Long updateUser) {
        //处理时间
        Date date = DateUtil.formEndTime(DateUtil.formatStrToDate(dto.getEndTime()));
        dto.setEndTime(DateUtil.formatDateTime(date));

        //新增房车基本参数
        if (null != dto.getCarParamDtoList() && dto.getCarParamDtoList().size() > 0) {
            //判断自定义参数不能超过10个
            ParamDto p = new ParamDto();
            p.setMode(GoodsParam.PARAM_COMMON);
            List<ParamDto> ps = paramService.selectList(p);
            Ensure.that(dto.getCarParamDtoList().size()).isNotGt(10 + ps.size(), "00020002");

            for (CarParamDto carParamDto : dto.getCarParamDtoList()) {
                //如果这条为自定义参数
                Long paramId = 0L;
                Ensure.that(carParamDto.getParamDto().getMode()).isNotNull("00090005");
                if (carParamDto.getParamDto().getMode() == GoodsParam.PARAM_CUSTOM) {
                    //自定义参数,新增
                    ParamDto paramDto = carParamDto.getParamDto();
                    paramDto.setMode(2);
                    paramDto.setSourceId(id);
                    paramDto.setSourceType(GoodsParam.CAR_RENT);
                    if (createUser != null) {
                        paramDto.setCreateUser(createUser);
                    }
                    if (updateUser != null) {
                        paramDto.setUpdateUser(updateUser);
                    }
                    paramId = paramService.add(paramDto);
                } else if (carParamDto.getParamDto().getMode() == GoodsParam.PARAM_COMMON) {
                    //公共参数,查询
                    ParamDto paramDto = paramService.selectBySourceId(carParamDto.getParamDto());
                    if (null != paramDto) {
                        paramId = paramDto.getId();
                    }
                } else {
                    return false;
                }
                carParamDto.setParamId(paramId);
                carParamDto.setSourceType(GoodsParam.CAR_RENT);
                carParamDto.setSourceId(id);
                if (null != createUser) {
                    carParamDto.setCreateUser(createUser);
                }
                if (null != updateUser) {
                    carParamDto.setUpdateUser(updateUser);
                }
                carParamService.add(carParamDto);
            }
        }
        //新增车内配置(固定/自定义)
        //自定义数量最多10个
        ConfigDto c = new ConfigDto();
        c.setMode(GoodsParam.PARAM_COMMON);
        PageInfo<ConfigDto> cs = configService.selectList(new Pager(), c);
        Ensure.that(dto.getCarConfigDtoList().size()).isNotGt(10 + cs.getList().size(), "00020003");

        List<CarConfigDto> carConfigDtoList = dto.getCarConfigDtoList();
        if (null != carConfigDtoList && carConfigDtoList.size() > 0) {
            //新增到配置表
            for (CarConfigDto carConfigDto : carConfigDtoList) {
                Long configId = 0L;
                String configName = "111";
                Ensure.that(carConfigDto.getConfigDto().getMode()).isNotNull("00090005");
                if (carConfigDto.getConfigDto().getMode() == GoodsParam.PARAM_CUSTOM) {
                    //如果这条为自定义配置
                    ConfigDto configDto = carConfigDto.getConfigDto();
                    configDto.setMode(2);
                    configDto.setSourceType(GoodsParam.CAR_RENT);
                    configDto.setCarId(id);
                    if (null != createUser) {
                        configDto.setCreateUser(createUser);
                    }
                    if (null != updateUser) {
                        configDto.setUpdateUser(updateUser);
                    }
                    configId = configService.add(configDto);
                    configName = configDto.getName();
                } else if (carConfigDto.getConfigDto().getMode() == GoodsParam.PARAM_COMMON) {
                    //公共参数
                    ConfigDto configDto = configService.selectByPrimaryKey(carConfigDto.getConfigDto().getId());
                    if (configDto != null) {
                        configId = configDto.getId();
                        configName = configDto.getName();
                    }
                } else {
                    return false;
                }

                carConfigDto.setConfigId(configId);
                carConfigDto.setSourceType(GoodsParam.CAR_RENT);
                carConfigDto.setSourceId(id);
                carConfigDto.setName(configName);
                if (null != createUser) {
                    carConfigDto.setCreateUser(createUser);
                }
                if (null != updateUser) {
                    carConfigDto.setUpdateUser(updateUser);
                }
                carConfigService.add(carConfigDto);
            }
        }

        //新增车型主图
        if (null != dto.getCarImageDtoList() && dto.getCarImageDtoList().size() > 0) {
            for (CarImageDto carImageDto : dto.getCarImageDtoList()) {
                carImageDto.setSourceType(GoodsParam.CAR_RENT);
                carImageDto.setSourceId(id);
                if (null != createUser) {
                    carImageDto.setCreateUser(createUser);
                }
                if (null != updateUser) {
                    carImageDto.setUpdateUser(updateUser);
                }
                carImageService.add(carImageDto);
            }
        }

        //新增车型视频
        if (null != dto.getCarVideoDtoList() && dto.getCarVideoDtoList().size() > 0) {
            for (CarVideoDto carVideoDto : dto.getCarVideoDtoList()) {
                carVideoDto.setSourceType(GoodsParam.CAR_RENT);
                carVideoDto.setSourceId(id);
                if (null != createUser) {
                    carVideoDto.setCreateUser(createUser);
                }
                if (null != updateUser) {
                    carVideoDto.setUpdateUser(updateUser);
                }
                carVideoService.add(carVideoDto);
            }
        }

        //保险单图片表
        if (null != dto.getInsuranceDtoList() && dto.getInsuranceDtoList().size() > 0) {
            for (InsuranceDto insuranceDto : dto.getInsuranceDtoList()) {
                insuranceDto.setSourceType(GoodsParam.CAR_RENT);
                insuranceDto.setSourceId(id);
                if (null != createUser) {
                    insuranceDto.setCreateUser(createUser);
                }
                if (null != updateUser) {
                    insuranceDto.setUpdateUser(updateUser);
                }
                insuranceService.add(insuranceDto);
            }
        }

        //保存为草稿
        if (null != dto.getDraft() && dto.getDraft() == 1) {
            CarDraftDto carDraftDto = DozerUtils.map(dto, CarDraftDto.class);
            carDraftDto.setName(dto.getDraftName());
            carDraftDto.setShopId(dto.getShopId());
            carDraftDto.setSourceType(GoodsParam.CAR_RENT);
            carDraftDto.setSourceId(id);
            if (null != createUser) {
                carDraftDto.setCreateUser(createUser);
            }
            if (null != updateUser) {
                carDraftDto.setUpdateUser(updateUser);
            }
            carDraftService.add(carDraftDto);
        }
        //设置外键属性新增到租赁表
        dto.setId(id);
        //如果在数据库中查询到该ID的为更新
        CarRentDto crDto = carRentService.selectById(id);
        if (crDto != null) {
            return carRentService.update(dto);
        } else {
            return carRentService.add(dto) > 0;
        }
    }

    /**
     * 假删
     *
     * @param mode
     * @param ids
     * @return
     */
    public Boolean del(Integer mode, String ids) {
        String[] idx = ids.trim().split(",");
        for (String id : idx) {
            GoodsDto goodsDto = goodsService.selectById(Long.valueOf(id));
            goodsDto.setDeleted(true);
            goodsService.updateById(goodsDto);
        }
        switch (mode) {
            case GoodsParam.CAR_SALE: {
                //房车销售
                for (String id : idx) {
                    CarSaleDto carSaleDto = carSaleService.selectById(Long.valueOf(id));
                    carSaleDto.setCarStatus(1);
                    carSaleDto.setDeleted(true);
                    carSaleService.update(carSaleDto);
                }
                break;
            }
            case GoodsParam.CAR_RENT: {
                //房车租赁
                for (String id : idx) {
                    CarRentDto carRentDto = carRentService.selectById(Long.valueOf(id));
                    carRentDto.setCarStatus(1);
                    carRentDto.setDeleted(true);
                    carRentService.update(carRentDto);
                }
                break;
            }
        }
        return true;
    }

    /**
     * 根据id查询商品详情
     *
     * @param mode,id
     * @return
     */
    public GoodsResponse selectById(Integer mode, Long id) {
        GoodsResponse response = new GoodsResponse();
        switch (mode) {
            case GoodsParam.CAR_SALE: {
                //房车销售
                CarSaleDto carSaleDto = carSaleService.selectById(id);
                Ensure.that(carSaleDto).isNotNull("00010007");
                //查询房车展示图
                CarImageDto carImageDto = new CarImageDto();
                carImageDto.setSourceId(id);
                carImageDto.setSourceType(2);
                List<CarImageDto> carImageDtoList = carImageService.selectList(carImageDto);
                carSaleDto.setCarImageDtoList(carImageDtoList);

                //查询房车视频
                CarVideoDto carVideoDto = new CarVideoDto();
                carVideoDto.setSourceId(id);
                carVideoDto.setSourceType(2);
                List<CarVideoDto> carVideoDtos = carVideoService.selectList(carVideoDto);
                carSaleDto.setCarVideoDtoList(carVideoDtos);

                //查询房车参数关联数据
                CarParamDto carParamDto = new CarParamDto();
                carParamDto.setSourceId(id);
                carParamDto.setSourceType(2);
                List<CarParamDto> carParamDtoList = carParamService.selectList(carParamDto);
                if (carParamDtoList != null && carParamDtoList.size() > 0) {
                    for (CarParamDto cpDto : carParamDtoList) {
                        ParamDto paramDto = paramService.selectById(cpDto.getParamId());
                        cpDto.setParamDto(paramDto);
                    }
                }
                carSaleDto.setCarParamDtoList(carParamDtoList);

                //查询车内配置详情数据
                CarConfigDto carConfigDto = new CarConfigDto();
                carConfigDto.setSourceId(id);
                carConfigDto.setSourceType(2);
                List<CarConfigDto> carConfigDtoList = carConfigService.selectList(carConfigDto);
                if (carConfigDtoList != null && carConfigDtoList.size() > 0) {
                    for (CarConfigDto ccDto : carConfigDtoList) {
                        ConfigDto configDto = configService.selectById(ccDto.getConfigId());
                        ccDto.setConfigDto(configDto);
                    }
                }
                carSaleDto.setCarConfigDtoList(carConfigDtoList);
                response.setCarSaleDto(carSaleDto);
                break;
            }
            case GoodsParam.CAR_RENT: {
                //房车租赁
                CarRentDto carRentDto = carRentService.selectById(id);
                Ensure.that(carRentDto).isNotNull("00010007");
                //查询房车展示图
                CarImageDto carImageDto = new CarImageDto();
                carImageDto.setSourceId(id);
                carImageDto.setSourceType(2);
                List<CarImageDto> carImageDtoList = carImageService.selectList(carImageDto);
                carRentDto.setCarImageDtoList(carImageDtoList);

                //查询房车视频
                CarVideoDto carVideoDto = new CarVideoDto();
                carVideoDto.setSourceId(id);
                carVideoDto.setSourceType(2);
                List<CarVideoDto> carVideoDtos = carVideoService.selectList(carVideoDto);
                carRentDto.setCarVideoDtoList(carVideoDtos);

                //查询房车参数关联数据
                CarParamDto carParamDto = new CarParamDto();
                carParamDto.setSourceId(id);
                carParamDto.setSourceType(2);
                List<CarParamDto> carParamDtoList = carParamService.selectList(carParamDto);
                if (carParamDtoList != null && carParamDtoList.size() > 0) {
                    for (CarParamDto cpDto : carParamDtoList) {
                        ParamDto paramDto = paramService.selectById(cpDto.getParamId());
                        cpDto.setParamDto(paramDto);
                    }
                }
                carRentDto.setCarParamDtoList(carParamDtoList);

                //查询车内配置详情数据
                CarConfigDto carConfigDto = new CarConfigDto();
                carConfigDto.setSourceId(id);
                carConfigDto.setSourceType(2);
                List<CarConfigDto> carConfigDtoList = carConfigService.selectList(carConfigDto);
                if (carConfigDtoList != null && carConfigDtoList.size() > 0) {
                    for (CarConfigDto ccDto : carConfigDtoList) {
                        ConfigDto configDto = configService.selectById(ccDto.getConfigId());
                        ccDto.setConfigDto(configDto);
                    }
                }

                //查询保险单照片
                InsuranceDto insuranceDto = new InsuranceDto();
                insuranceDto.setSourceType(2);
                insuranceDto.setSourceId(id);
                List<InsuranceDto> insuranceDtoList = insuranceService.selectList(insuranceDto);
                carRentDto.setInsuranceDtoList(insuranceDtoList);

                carRentDto.setCarConfigDtoList(carConfigDtoList);
                response.setCarRentDto(carRentDto);
                break;
            }
        }
        return response;
    }

    /**
     * 根据商品id修改商品
     *
     * @param dto
     * @return
     */
    public Boolean update(GoodsDto dto) {
        //修改商品表
        dto.setName(dto.getCarRentDto().getName());
        dto.setOrigPrice(dto.getCarRentDto().getPrice());
        dto.setSellPrice(dto.getCarRentDto().getPrice());
        dto.setImage(dto.getCarRentDto().getImage());
        dto.setDescribes(dto.getCarRentDto().getDescribes());
        dto.setRemark(dto.getCarRentDto().getRemark());
        dto.setDraft(dto.getCarRentDto().getDraft());
        goodsService.updateById(dto);
        switch (dto.getType()) {
            case GoodsParam.CAR_SALE: {
                //删除之前CarRentDto子表中存在的数据
                cleanCarMessage(dto.getCarRentDto().getId(), GoodsParam.CAR_SALE);
                //新增
                addCarSaleMessage(dto.getId(), dto.getCarSaleDto());
                break;
            }
            case GoodsParam.CAR_RENT: {
                //对car_rent表及其子表进行操作
                //删除之前CarRentDto子表中存在的数据
                cleanCarMessage(dto.getCarRentDto().getId(), GoodsParam.CAR_RENT);
                //新增
                addCarRentMessage(dto.getCarRentDto().getId(), dto.getCarRentDto(), null, dto.getUpdateUser());
                break;
            }
        }
        return true;
    }

    private void cleanCarMessage(Long id, Integer sourceType) {
        /*更新房车展示图*/
        //删除之前的房车展示图
        CarImageDto ciDto = new CarImageDto();
        ciDto.setSourceId(id);
        ciDto.setSourceType(sourceType);
        carImageService.delByKeyTrue(ciDto);
        //删除之前的房车视频
        CarVideoDto cvDto = new CarVideoDto();
        cvDto.setSourceId(id);
        cvDto.setSourceType(sourceType);
        carVideoService.delByKeyTrue(cvDto);
        /*房车参数关联数据*/
        //删除之前的房车参数关联数据
        CarParamDto carParamDto = new CarParamDto();
        carParamDto.setSourceId(id);
        carParamDto.setSourceType(sourceType);
        carParamService.delByKeyTrue(carParamDto);
        ParamDto paramDto = new ParamDto();
        paramDto.setSourceType(sourceType);
        paramDto.setSourceId(id);
        paramService.delByKeyTrue(paramDto);
        /*删除车内配置详情*/
        //删除之前的车内配置详情数据
        CarConfigDto carConfigDto = new CarConfigDto();
        carConfigDto.setSourceId(id);
        carConfigDto.setSourceType(sourceType);
        carConfigService.delByKeyTrue(carConfigDto);
        ConfigDto configDto = new ConfigDto();
        configDto.setSourceType(sourceType);
        configDto.setCarId(id);
        configService.delByKeyTrue(configDto);
        //删除保险单图片
        InsuranceDto insuranceDto = new InsuranceDto();
        insuranceDto.setSourceId(id);
        insuranceDto.setSourceType(sourceType);
        insuranceService.delByKeyTrue(insuranceDto);
    }

    /**
     * 分页查询商品
     *
     * @param pager
     * @param dto
     * @return
     */
    public Result selectList(Pager pager, GoodsDto dto,Integer sortType) {
        if (pager != null)
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        Ensure.that(dto.getType()).isNotNull("00010008");
        switch (dto.getType()) {
            case GoodsParam.CAR_SALE: {
                //房车销售
                PageInfo<CarSaleDto> pageInfo = carSaleService.selectList(pager, dto.getCarSaleDto());
                if (pageInfo != null && pageInfo.getList() != null) {
                    pager.setTotal(pageInfo.getTotal());
                    return new Result(pager, DozerUtils.maps(pageInfo.getList(), CarSaleResponse.class));
                }
                break;
            }
            case GoodsParam.CAR_RENT: {
                //房车租赁
                PageInfo<CarRentDto> pageInfo = carRentService.selectList(pager,dto.getCarRentDto(),sortType);
                if (pageInfo != null && pageInfo.getList() != null) {
                    pager.setTotal(pageInfo.getTotal());
                    for (CarRentDto carRentDto : pageInfo.getList()) {
                        //封装店铺名称
                        Result result = shopApi.findById(carRentDto.getShopId());
                        ShopDto shopDto = DozerUtils.map(result.getResult(), ShopDto.class);
                        if (null != shopDto && null != shopDto.getName()) {
                            carRentDto.setShopName(shopDto.getName());
                        }
                    }
                    return new Result(pager, DozerUtils.maps(pageInfo.getList(), CarRentResponse.class));
                }
                break;
            }
        }
        return new Result();
    }

    /**
     * 批量恢复到仓库
     *
     * @param mode
     * @param ids
     * @return
     */
    public Boolean recoveryById(Integer mode, String ids) {
        String[] idx = ids.trim().split(",");
        for (String id : idx) {
            GoodsDto goodsDto = goodsService.selectById(Long.valueOf(id));
            goodsDto.setDeleted(false);
            goodsService.updateById(goodsDto);
        }
        switch (mode) {
            case GoodsParam.CAR_SALE: {
                //房车销售
                for (String id : idx) {
                    CarSaleDto carSaleDto = carSaleService.selectById(Long.valueOf(id));
                    carSaleDto.setCarStatus(1);
                    carSaleDto.setDeleted(false);
                    carSaleService.update(carSaleDto);
                }
                break;
            }
            case GoodsParam.CAR_RENT: {
                //房车租赁
                for (String id : idx) {
                    CarRentDto carRentDto = carRentService.selectById(Long.valueOf(id));
                    carRentDto.setCarStatus(1);
                    carRentDto.setDeleted(false);
                    carRentService.update(carRentDto);
                }
                break;
            }
        }
        return true;
    }

    /**
     * 批量共享至供销平台
     *
     * @param mode
     * @param ids
     * @return
     */
    public Boolean addToShare(Integer mode, String ids) {
        String[] idx = ids.trim().split(",");
        switch (mode) {
            case GoodsParam.CAR_SALE: {
                //房车销售
                for (String id : idx) {
                    CarSaleDto carSaleDto = carSaleService.selectById(Long.valueOf(id));
                    carSaleDto.setShare(1);
                    carSaleService.update(carSaleDto);
                }
                break;
            }
            case GoodsParam.CAR_RENT: {
                //房车租赁
                for (String id : idx) {
                    CarRentDto carRentDto = carRentService.selectById(Long.valueOf(id));
                    carRentDto.setShare(1);
                    carRentService.update(carRentDto);
                }
                break;
            }
        }
        return true;
    }

    /**
     * 移除供销平台
     *
     * @param mode
     * @param ids
     * @return
     */
    public Boolean removeShare(Integer mode, String ids) {
        String[] idx = ids.trim().split(",");
        switch (mode) {
            case GoodsParam.CAR_SALE: {
                //房车销售
                for (String id : idx) {
                    CarSaleDto carSaleDto = carSaleService.selectById(Long.valueOf(id));
                    carSaleDto.setShare(0);
                    carSaleService.update(carSaleDto);
                }
                break;
            }
            case GoodsParam.CAR_RENT: {
                //房车租赁
                for (String id : idx) {
                    CarRentDto carRentDto = carRentService.selectById(Long.valueOf(id));
                    carRentDto.setShare(0);
                    carRentService.update(carRentDto);
                }
                break;
            }
        }
        return true;
    }

    /**
     * 从回收站中删除数据
     *
     * @param mode
     * @param ids
     * @return
     */
    public Boolean deleteByKey(Integer mode, String ids) {
        String[] idx = ids.trim().split(",");
        for (String id : idx) {
            goodsService.deleteByKey(Long.valueOf(id));
        }
        switch (mode) {
            case GoodsParam.CAR_SALE: {
                //房车销售
                for (String id : idx) {
                    cleanCarMessage(Long.valueOf(id), GoodsParam.CAR_SALE);
                    carSaleService.delById(Long.valueOf(id));
                }
                break;
            }
            case GoodsParam.CAR_RENT: {
                //房车租赁
                for (String id : idx) {
                    cleanCarMessage(Long.valueOf(id), GoodsParam.CAR_RENT);
                    carRentService.deleteByKey(Long.valueOf(id));
                }
                break;
            }
        }
        return true;
    }

    public String getCode(Integer typeId, Long shopId) {
        GoodsDto goodsDto = goodsService.selectLast(shopId);
        String lastCode = goodsDto.getCode().substring(12);
        int i = Integer.parseInt(lastCode) + 1;
        StringBuilder sb = new StringBuilder();
        DecimalFormat df1 = new DecimalFormat("00000000");
        String shop = df1.format(shopId);
        DecimalFormat df2 = new DecimalFormat("000");
        String type = df2.format(typeId);
        DecimalFormat df3 = new DecimalFormat("000000");
        String inc = df3.format(i);
        sb.append("3").append(type).append(shop).append(inc);
        return sb.toString();
    }
/**************app端*****************************/
    /**
     * app端查询子表列表
     * @param pager
     * @param dto
     * @return
     */
    public Result findList(Pager pager, GoodsAppDto dto,Integer sortType) {
        if (pager != null)
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        switch (dto.getType()) {
            case GoodsParam.CAR_SALE: {
                //房车销售
                PageInfo<CarSaleDto> pageInfo = carSaleService.selectList(pager, dto.getCarSaleDto());
                if (pageInfo != null && pageInfo.getList() != null) {
                    pager.setTotal(pageInfo.getTotal());
                    return new Result(pager, DozerUtils.maps(pageInfo.getList(), CarSaleResponse.class));
                }
                break;
            }
            case GoodsParam.CAR_RENT: {
                //房车租赁
                PageInfo<CarRentAppDto> pageInfo = carRentService.findList(pager, dto.getCarRentAppDto(),sortType);
                if (pageInfo != null && pageInfo.getList() != null) {
                    pager.setTotal(pageInfo.getTotal());
                    for (CarRentAppDto carRentAppDto:pageInfo.getList()) {
                        //设置关键参数
                        List<CarParamAppDto> core = paramFacade.findCore(carRentAppDto.getId());
                        carRentAppDto.setCarParamAppDtos(core);
                        //封装店铺名称
                        Result result = shopApi.findById(carRentAppDto.getShopId());
                        ShopDto shopDto = DozerUtils.map(result.getResult(), ShopDto.class);
                        if (null != shopDto && null != shopDto.getName()) {
                            carRentAppDto.setShopName(shopDto.getName());
                        }
                    }
                    return new Result(pager, DozerUtils.maps(pageInfo.getList(), CarRentAppResponse.class));
                }
                break;
            }
        }
        return new Result();
    }


    /**
     * app端根据id查询商品详情
     *
     * @param mode,id
     * @return
     */
    public GoodsAppResponse findById(Integer mode, Long id) {
        GoodsAppResponse response = new GoodsAppResponse();
        switch (mode) {
            case GoodsParam.CAR_SALE: {
                //房车销售
                CarSaleAppDto carSaleDto = carSaleService.findById(id);
                Ensure.that(carSaleDto).isNotNull("00010007");
                //查询房车展示图
                CarImageDto carImageDto = new CarImageDto();
                carImageDto.setSourceId(id);
                carImageDto.setSourceType(2);
                List<CarImageDto> carImageDtoList = carImageService.selectList(carImageDto);
                carSaleDto.setCarImageDtoList(carImageDtoList);

                //查询房车参数关联数据
                CarParamDto carParamDto = new CarParamDto();
                carParamDto.setSourceId(id);
                carParamDto.setSourceType(2);
                List<CarParamDto> carParamDtoList = carParamService.selectList(carParamDto);
                if (carParamDtoList != null && carParamDtoList.size() > 0) {
                    for (CarParamDto cpDto : carParamDtoList) {
                        ParamDto paramDto = paramService.selectById(cpDto.getParamId());
                        cpDto.setParamDto(paramDto);
                    }
                }
                carSaleDto.setCarParamDtoList(carParamDtoList);

                //查询车内配置详情数据
                CarConfigDto carConfigDto = new CarConfigDto();
                carConfigDto.setSourceId(id);
                carConfigDto.setSourceType(2);
                List<CarConfigDto> carConfigDtoList = carConfigService.selectList(carConfigDto);
                if (carConfigDtoList != null && carConfigDtoList.size() > 0) {
                    for (CarConfigDto ccDto : carConfigDtoList) {
                        ConfigDto configDto = configService.selectById(ccDto.getConfigId());
                        ccDto.setConfigDto(configDto);
                    }
                }
                carSaleDto.setCarConfigDtoList(carConfigDtoList);
                response.setCarSaleAppDto(carSaleDto);
                break;
            }
            case GoodsParam.CAR_RENT: {
                //房车租赁
                CarRentAppDto carRentDto = carRentService.findById(id);
                ShopDto shopDto = DozerUtils.map(shopApi.findById(carRentDto.getShopId()).getResult(), ShopDto.class);
                if (null != shopDto && null != shopDto.getName()) {
                    carRentDto.setShopName(shopDto.getName());
                    carRentDto.setCantactPhone(shopDto.getCantactPhone());
                    carRentDto.setShopLogo(shopDto.getLogo());
                }
                Ensure.that(carRentDto).isNotNull("00010007");
                //查询房车展示图
                CarImageDto carImageDto = new CarImageDto();
                carImageDto.setSourceId(id);
                carImageDto.setSourceType(2);
                List<CarImageDto> carImageDtoList = carImageService.selectList(carImageDto);
                carRentDto.setCarImageDtoList(carImageDtoList);

                //查询房车展示图
                CarVideoDto carVideoDto = new CarVideoDto();
                carVideoDto.setSourceId(id);
                carVideoDto.setSourceType(2);
                List<CarVideoDto> carVideoDtos = carVideoService.selectList(carVideoDto);
                carRentDto.setCarVideoDtoList(carVideoDtos);

                //查询房车参数关联数据
                CarParamDto carParamDto = new CarParamDto();
                carParamDto.setSourceId(id);
                carParamDto.setSourceType(2);
                List<CarParamDto> carParamDtoList = carParamService.selectList(carParamDto);
                if (carParamDtoList != null && carParamDtoList.size() > 0) {
                    for (CarParamDto cpDto : carParamDtoList) {
                        ParamDto paramDto = paramService.selectById(cpDto.getParamId());
                        cpDto.setParamDto(paramDto);
                    }
                }
                carRentDto.setCarParamDtoList(carParamDtoList);

                //查询车内配置详情数据
                CarConfigDto carConfigDto = new CarConfigDto();
                carConfigDto.setSourceId(id);
                carConfigDto.setSourceType(2);
                List<CarConfigDto> carConfigDtoList = carConfigService.selectList(carConfigDto);
                if (carConfigDtoList != null && carConfigDtoList.size() > 0) {
                    for (CarConfigDto ccDto : carConfigDtoList) {
                        ConfigDto configDto = configService.selectById(ccDto.getConfigId());
                        ccDto.setConfigDto(configDto);
                    }
                }

                //查询保险单照片
                InsuranceDto insuranceDto = new InsuranceDto();
                insuranceDto.setSourceType(2);
                insuranceDto.setSourceId(id);
                List<InsuranceDto> insuranceDtoList = insuranceService.selectList(insuranceDto);
                carRentDto.setInsuranceDtoList(insuranceDtoList);

                carRentDto.setCarConfigDtoList(carConfigDtoList);
                response.setCarRentAppDto(carRentDto);
                break;
            }
        }
        return response;
    }
}
